package com.nightpiter.view.dialogs;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.View;
import android.view.ViewInflate;

import com.nightpiter.R;
import com.nightpiter.model.MetroModelData;

/**
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
public class AboutDialog implements OnClickListener {

	/** the dialog itself */
	private AlertDialog dialog;

	/** builder for the dialog */
	private AlertDialog.Builder builder;

	/** about application content view */
	private View contentView;

	/** data to show info about */
	private MetroModelData modelData;

	/**
	 * public constructor
	 * 
	 * @param context
	 */
	public AboutDialog(Context context) {
		this.contentView = (View) ViewInflate.from(context).inflate(
				R.layout.about, null, null);

		this.builder = new AlertDialog.Builder(context);
		this.builder.setTitle(R.string.about_title);
		this.builder.setIcon(R.drawable.matmex_small);
		this.builder.setView(this.contentView);
		this.builder.setPositiveButton("Hide", this);
		this.builder.setNegativeButton("Map info", this);

	}

	/**
	 * show the dialog if it has not already shown
	 */
	public void show(MetroModelData modelData) {
		if (this.dialog == null) {
			this.modelData = modelData;
			this.dialog = this.builder.show();
		}
	}

	/**
	 * hide the dialog if it is visible now
	 */
	public void hide() {
		if (this.dialog != null) {
			this.dialog.dismiss();
			this.dialog = null;
		}
	}

	/**
	 * handles button clicks
	 */
	public void onClick(DialogInterface dialog, int button) {
		if (dialog == this.dialog) {
			switch (button) {
			case DialogInterface.BUTTON2:
				AlertDialog.Builder mapDialogBuilder = new AlertDialog.Builder(
						this.dialog.getContext());
				mapDialogBuilder.setTitle("Map info");
				mapDialogBuilder.setPositiveButton("Hide", this);
				String message = "";
				if (modelData.getCityName().length() == 0) {
					message += "City name: not presented\n";
				} else {
					message += "City name: " + modelData.getCityName() + "\n";
				}
				if (modelData.getAuthors().size() == 0) {
					message += "Authors: not presented\n";
				} else {
					message += "Authors:\n";
					for (String author : modelData.getAuthors()) {
						message += "    " + author + "\n";
					}
				}
				message += "\nthe Model statistics:\n";
				message += "   stations: " + modelData.getStations().size()
						+ "\n";
				message += "   lines: " + modelData.getLines().size() + "\n";
				message += "   hubs: " + modelData.getHubs().size() + "\n";
				message += "   tunnels: " + modelData.getTunnels().size()
						+ "\n";
				message += "   transfers: " + modelData.getTransfers().size()
						+ "\n";
				if (modelData.getComment().length() == 0) {
					message += "\nComment: not presented\n";
				} else {
					message += "\nComment: " + modelData.getComment() + "\n";
				}

				mapDialogBuilder.setMessage(message);

				mapDialogBuilder.show();
				break;

			default:
				break;
			}
			hide();

		}
	}
}
